﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using KTD.Web.Models;
using XXS.Common.BaseDataAccessLayer;

namespace KTD.Web.BLL
{
    public class MessageManager
    {


        private readonly IBaseDAL<SysVerifyCode> _vcDal;
        public MessageManager()
        {
            _vcDal = new BaseDAL<SysVerifyCode>(new DB());
        }

        public bool IsMobile(string mobile)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(mobile, @"^(13|14|15|16|18|19|17)\d{9}$"))
                return true;
            else
            {
                throw new Exception("您手机格式不正确,请填写正确的手机号码!");
            }
        }

        public void sendCodeMessage(string Tel,string Code)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "LTAI4FtsPu2uSdKgdk4XoqFr", "lmwwMema2Kj4c9QlDmzGrlE3AKs9hq");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", Tel);
            request.AddQueryParameters("SignName", "住哪儿");
            request.AddQueryParameters("TemplateCode", "SMS_183451056");
            request.AddQueryParameters("TemplateParam", "{'code':'"+Code+"'}");
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }

        }


        public void sendMessage(string Tel, string TemplateCode, string TemplateParam)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "LTAI4FtsPu2uSdKgdk4XoqFr", "lmwwMema2Kj4c9QlDmzGrlE3AKs9hq");
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;
            request.Domain = "dysmsapi.aliyuncs.com";
            request.Version = "2017-05-25";
            request.Action = "SendSms";
            // request.Protocol = ProtocolType.HTTP;
            request.AddQueryParameters("PhoneNumbers", Tel);
            request.AddQueryParameters("SignName", "住哪儿");
            request.AddQueryParameters("TemplateCode", TemplateCode);
            request.AddQueryParameters("TemplateParam", TemplateParam);
            try
            {
                CommonResponse response = client.GetCommonResponse(request);
                Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
            }
            catch (ServerException e)
            {
                Console.WriteLine(e);
            }
            catch (ClientException e)
            {
                Console.WriteLine(e);
            }

        }


        /// <summary>
        /// 创建验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public void CreateCode(string mobile)
        {

            if (_vcDal.Exist(n => n.Mobile == mobile && n.ExpiredDateTime > DateTime.Now))
            {
                throw new Exception("短信验证码在有效时间内，请不要重复发送!");

            }
            else
            {
                CreateLoginCode(mobile);

            }
        }
        /// <summary>
        /// 创建验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public void CreateLoginCode(string mobile)
        {
            Random rd = new Random();
            var model = new SysVerifyCode
            {
                EffectiveMinite = 2,
                ExpiredDateTime = DateTime.Now.AddMinutes(2),
                Mobile = mobile,
                GenerateVerifyCodeType = GenerateVerifyCodeType.登录,
                VerifyCode = rd.Next(100000, 999999).ToString()
            };
            model.SMSContent = $"尊敬的用户,您的登录验证码为：{model.VerifyCode}，有效期为2分钟！";
            _vcDal.Create(model);

            sendCodeMessage(mobile, model.VerifyCode);
        }
    }
}